/*
*	Project: Aethon
*
*	File: Terrain.cpp
*
*	Author: Caleb Reinking
*
*	Date: 2/15/2009
*
*   Purpose: The Terrain is a crucial part of the final environment. The
*            terrain will be 3D texture mapped and contain the ability
*            to interact with model collision spheres to obtain a realistic
*            way of simulating gravity and ground collision.
*            Terrain Class Implementation File
*/

#include "../include/Terrain.h"
#include "../include/CollisionSphere.h"
#include <fstream>
#include <iostream>

using namespace std;

	
//-------------------------------------------------------------------------	
//------------------- Methods ---------------------------------------------
//##########################################################################
	int Terrain::collidesWith( CollisionSphere sphere )
	{
	   cout << "Check to see if the sphere collides with a certain face "
	        << "of the terrain" << endl;
		return 0;
	} // end collidesWith
	
//##########################################################################
int Terrain::load( string heightFileName, string texFileName )
{
    //cout << "Trying to load the terrain" << endl;
    ifstream inputFile;
    string inputFileName = "../data/maps/" + heightFileName + ".txt";
    modPart.processImage( texFileName );    
    //cout << "load the file stored in string fileName." << endl;
    
    inputFile.open( inputFileName.c_str() );
    if( !inputFile )
    {
        cout << "ERRORRRRRR!!!! " << endl;
        exit(0);
    }
    //cout << "File loaded successfully" << endl;
    
    inputFile >> dimension;
    cout << "dimension = " << dimension << endl;
    inputFile >> separationFact;
    cout << "seperationFactor = " << separationFact << endl;
    
    string temp;
    for( int x = 0; x<dimension ; x++ )
    {
    	 for( int y = 0; y < dimension ; y++ )
    	 {
    		  inputFile >> temp;
    		  heightGrid[x][y] = atof(temp.c_str())/20.0;
    		  // cout << heightGrid[x][y] << " ";
    	 }
    	 // cout << endl;
    }
    
    
    return 0;
    
} // end load
	
//##########################################################################
void Terrain::display()
{
   //cout << "Draws the terrain to the screen using openGL commands" << endl;
  
   
   modPart.loadTexture();
   
   float f_dim = dimension/1.0;
   
   glPushMatrix();

   glTranslatef( -25.0, 0.0, -25.0 );
   glColor3ub( 255, 255, 255 );
   
   glBegin(GL_QUADS);
	   for( int i = dimension-1 ; i > 0 ; i-- )
	   {
		  for( int j = 0 ; j < dimension-1 ; j++ )
		  {
			 glTexCoord2d( (i)/f_dim,(j+1)/f_dim);  
			 glVertex3f(i*separationFact, heightGrid[i][j]/1.0, j*separationFact );
			 
			 glTexCoord2d( (i-1)/f_dim,(j+1)/f_dim); 
			 glVertex3f((i)*separationFact, heightGrid[i][j+1], (j+1)*separationFact );
			 
			 glTexCoord2d( (i-1)/f_dim,(j)/f_dim); 
			 glVertex3f((i-1)*separationFact, heightGrid[i-1][j+1], (j+1)*separationFact );
			 
			 glTexCoord2d(i/f_dim ,j/f_dim); 
			 glVertex3f((i-1)*separationFact, heightGrid[i-1][j], (j)*separationFact );
		  }
		}
	glEnd();
	
	
	glPopMatrix();
	
	modPart.freeTexture();
   
   
} // end display
	
